package com.flyox.game.militarychess.client.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.flyox.game.militarychess.CONSTS;
import com.flyox.game.militarychess.client.control.MainControl;
import com.flyox.game.militarychess.message.ChessPackage;
import com.flyox.game.militarychess.message.Request;
import com.flyox.game.militarychess.util.SerialUtil;

public class ManagerJFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = -2220973764129356988L;
	private JTextField jTextFieldDeskNum;
	private JTextField jTextFieldSeatName;
	private JButton jButtonShowUsers;
	private JButton jButtonConnect;
	private JButton jButtonLayout;
	MainControl mc=new MainControl();
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ManagerJFrame inst = new ManagerJFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ManagerJFrame() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jTextFieldDeskNum = new JTextField();
				getContentPane().add(jTextFieldDeskNum);
				jTextFieldDeskNum.setText("0");
				jTextFieldDeskNum.setBounds(18, 48, 62, 16);
			}
			{
				jTextFieldSeatName = new JTextField();
				getContentPane().add(jTextFieldSeatName);
				jTextFieldSeatName.setText("S1");
				jTextFieldSeatName.setBounds(97, 47, 62, 16);
			}
			{
				jButtonLayout = new JButton();
				getContentPane().add(jButtonLayout);
				jButtonLayout.setText("\u67e5\u770b\u5e03\u5c40");
				jButtonLayout.setBounds(184, 45, 95, 16);
				jButtonLayout.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButtonLayoutActionPerformed(evt);
					}
				});
			}
			{
				jButtonConnect = new JButton();
				getContentPane().add(jButtonConnect);
				jButtonConnect.setText("\u94fe\u63a5");
				jButtonConnect.setBounds(18, 7, 79, 22);
				jButtonConnect.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButtonConnectActionPerformed(evt);
					}
				});
			}
			{
				jButtonShowUsers = new JButton();
				getContentPane().add(jButtonShowUsers);
				jButtonShowUsers.setText("\u663e\u793a\u7528\u6237");
				jButtonShowUsers.setBounds(18, 81, 79, 22);
				jButtonShowUsers.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButtonShowUsersActionPerformed(evt);
					}
				});
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButtonLayoutActionPerformed(ActionEvent evt) {
		ChessPackage cp = new ChessPackage(CONSTS.CMD_MANAGER);
		Request request = new Request();
		request.setCommand(CONSTS.CMD_MANAGER_LAYOUT);
		request.setDeskNum(Integer.parseInt(jTextFieldDeskNum.getText()));
		byte[] serRequest = null;
		try {
			serRequest = SerialUtil.enSerial(request);
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
		cp.setContent(serRequest);
		// 发送请求
		mc.sendRequest(cp);
	}
	
	private void jButtonConnectActionPerformed(ActionEvent evt) {
		mc.managerLogin("127.0.0.1", 8808);
	}
	
	private void jButtonShowUsersActionPerformed(ActionEvent evt) {
		ChessPackage cp = new ChessPackage(CONSTS.CMD_MANAGER);
		Request request = new Request();
		request.setCommand(CONSTS.CMD_MANAGER_USERS);
		byte[] serRequest = null;
		try {
			serRequest = SerialUtil.enSerial(request);
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
		cp.setContent(serRequest);
		// 发送请求
		mc.sendRequest(cp);
	}

}
